﻿<ValidateForm Model="@ValidateModel">
    <EditorForm TModel="Dummy">
        <FieldItems>
            <EditorItem @bind-Field="@Model.BirthDay" Readonly="true" />
            <EditorItem @bind-Field="@Model.Hobby">
                <EditTemplate Context="value">
                    @{
                        var model = value as Dummy;

                        // 此处也可以不使用 Context 值，直接使用模型 ValidateModel 即可
                    }
                    <div class="form-group col-12">
                        <CheckboxList TModel="SelectedItem" TValue="IEnumerable<string>" @bind-Value="@model!.Hobby" Items="@Hobbys"
                                      ValueField="Value" TextField="Text" CheckedField="Active">
                            <RequiredValidator ErrorMessage="至少选择一种爱好" />
                        </CheckboxList>
                    </div>
                </EditTemplate>
            </EditorItem>
        </FieldItems>
        <Buttons>
            <Button ButtonType="ButtonType.Submit" Icon="fa fa-save" Text="提交" />
        </Buttons>
    </EditorForm>
</ValidateForm>

@code {
    private Dummy ValidateModel { get; set; } = new Dummy()
    {
        Name = "张三",
        Age = 23,
        BirthDay = new DateTime(1997, 12, 05),
        Education = EnumEducation.Middel
    };

    public class Dummy
    {
        /// <summary>
        /// 
        /// </summary>
        [Required(ErrorMessage = "姓名不可以为空")]
        [DisplayName("姓名")]
        [EditorOrder(1)]
        public string? Name { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [Required(ErrorMessage = "年龄不可以为空")]
        [DisplayName("年龄")]
        [EditorOrder(2)]
        public int Age { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DisplayName("生日")]
        [EditorOrder(3)]
        public DateTime BirthDay { get; set; } = DateTime.Today.AddYears(-20);

        /// <summary>
        /// 
        /// </summary>
        [Required(ErrorMessage = "请选择一种爱好")]
        [DisplayName("爱好")]
        [EditorOrder(5)]
        public IEnumerable<string> Hobby { get; set; } = new List<string>();

        /// <summary>
        /// 
        /// </summary>
        [Required(ErrorMessage = "请选择学历")]
        [DisplayName("学历")]
        [EditorOrder(4)]
        public EnumEducation? Education { get; set; }
    }
}